﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="HttpContextCacheManager.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using INNOVACALL.Framework.Common.Properties;

namespace INNOVACALL.Framework.Common.MVCHelpers
{
    /// <summary>
    /// Modes de gestion du cache du navigateur.
    /// </summary>
	public enum enCacheMode
	{
        /// <summary>
        /// See http://stackoverflow.com/questions/1160105/asp-net-mvc-disable-browser-cache
        /// Following values must be stored in AppSettings.config
        /// ExpirationDate (int: days),
        /// ValidUntilExpires (true, false),
        /// Revalidation (AllCaches, ProxyCaches, None)
        /// Cacheability (NoCache, Private, Server, ServerAndNoCache, Public, ServerAndPrivate)
        /// </summary>
		ConfigSource,
        /// <summary>
        /// Cache desactivé.
        /// </summary>
		NoCache,
        /// <summary>
        /// Navigateur par default.
        /// </summary>
		CacheEnabled
	}

    /// <summary>
    /// ASP.net Controllers / View cache management
    /// </summary>
	public class CacheManager : ActionFilterAttribute
	{
        /// <summary>
        /// The e method base
        /// </summary>
		private string eMethodBase = "INNOVACALL.Framework.Common.MVCHelpers";
        /// <summary>
        /// Gets or sets the expiration date.
        /// </summary>
        /// <value>The expiration date.</value>
		public DateTime ExpirationDate { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether [valid until expires].
        /// </summary>
        /// <value><c>true</c> if [valid until expires]; otherwise, <c>false</c>.</value>
		public bool ValidUntilExpires { get; set; }
        /// <summary>
        /// Gets or sets the revalidation.
        /// </summary>
        /// <value>The revalidation.</value>
		public HttpCacheRevalidation Revalidation { get; set; }
        /// <summary>
        /// Gets or sets the cacheability.
        /// </summary>
        /// <value>The cacheability.</value>
		public HttpCacheability Cacheability { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="CacheManager"/> class.
        /// </summary>
        /// <param name="mode">The mode.</param>
        /// <exception cref="INNOVACALL.Framework.Common.InnovacallExceptions.CommonException"></exception>
		public CacheManager(enCacheMode mode)
			: base()
        {
            object eParameters = new { @mode = mode.ToString()};
			string ExpirationWebConfig = String.Empty;
            switch (mode)
            {
                case enCacheMode.ConfigSource:
                    ExpirationWebConfig = ConfigurationManager.AppSettings["ControllerCacheExpirationTimout"];
                    DateTime currentDate;
                    if (!DateTime.TryParse(ExpirationWebConfig, out currentDate)) {
						throw new InnovacallExceptions.CommonException(eMethodBase + ".CacheManagerConstructor",eParameters,Resources.ErrMsg_MVCHelpers_ConfigFileExpirationDateNotValid);											
					}
                    ExpirationDate = currentDate;
                    break;
                case enCacheMode.NoCache:
                    ExpirationDate = DateTime.UtcNow.AddDays(-1);
                    ValidUntilExpires = false;
                    Revalidation = HttpCacheRevalidation.AllCaches;
                    Cacheability = HttpCacheability.NoCache;
                    break;
                case enCacheMode.CacheEnabled:
                    break;
            }
        }

        /// <summary>
        /// see http://msdn.microsoft.com/en-us/library/dd381609.aspx
        /// see http://stackoverflow.com/questions/1160105/asp-net-mvc-disable-browser-cache
        /// </summary>
        /// <param name="filterContext">Contexte du filtre.</param>
		public override void OnResultExecuting(ResultExecutingContext filterContext)
		{
			filterContext.HttpContext.Response.Cache.SetExpires(this.ExpirationDate);
			filterContext.HttpContext.Response.Cache.SetValidUntilExpires(this.ValidUntilExpires);
			filterContext.HttpContext.Response.Cache.SetRevalidation(this.Revalidation);
			filterContext.HttpContext.Response.Cache.SetCacheability(this.Cacheability);
			if (Cacheability == HttpCacheability.NoCache)
			{
				filterContext.HttpContext.Response.Cache.SetNoStore();
			}

			base.OnResultExecuting(filterContext);
		}
	}
}
